import java.lang.reflect.Array;
import java.util.*;

public class ArrayListTest {
    public static void main(String[] args) {
        List list = new ArrayList<>();
        //show_1(list);

        show_2(list);

        show_3();

        show_4();
    }

    public static void show_1(List list){
        list.add("abc1");
        list.add("abc2");
        list.add("abc3");
        list.add("abc4");
        System.out.println(list);

        //插入元素
        list.add(1,"abc8");
        System.out.println(list);

        //删除元素
        System.out.println("remove:"+list.remove(2));

        //修改元素
        System.out.println("set:"+list.set(1,"abc6"));

        //获取元素
        System.out.println("get:"+list.get(2));

        System.out.println(list);

        //遍历元素 - list特有的遍历元素方式之一
        for (int i = 0;i < list.size();i++){
            System.out.println(list.get(i));
        }
    }

    public static void show_2(List list){
        list.add("abc1");
        list.add("abc2");
        list.add("abc3");
        list.add("abc4");
        System.out.println(list);


        /**
         * java.util.ConcurrentModificationException
         * 在迭代器过程中，不要使用集合操作元素，容易出现异常
         * 可以使用Iterator接口的子接口ListIterator来完成在迭代器过程中，使用集合操作元素
         * 当方法检测到对象的并发修改时，不允许切快速失败，抛出异常
         * 原因是迭代器不知道list的长度已改变
         */
        /*
        Iterator it = list.iterator();
        while(it.hasNext()){
            Object obj = it.next();
            if (obj.equals("abc2")){
                list.add("abc9");
            }
        }
        */

        ListIterator it = list.listIterator();
        //ListIterator可以实现在迭代器过程中，对集合进行增删改查
        while(it.hasNext()){
            Object obj = it.next();
            if (obj.equals("abc2")){
                it.add("abc9");
            }
        }
        System.out.println(list);
    }

    public static void show_3(){
        ArrayList arrayList = new ArrayList();
        arrayList.add(new Person("aaa",12));
        arrayList.add(new Person("bbb",13));
        arrayList.add(new Person("ccc",14));
        //arrayList.add("ddd");
        //Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to Person

        Iterator it = arrayList.iterator();
        while (it.hasNext()){
            Person p = (Person)it.next();
            System.out.println(p.getName()+":"+p.getAge());
        }
    }

    //去重
    public static void show_4(){
        ArrayList arrayList = new ArrayList();
//        arrayList.add("111");
//        arrayList.add("222");
//        arrayList.add("222");
//        arrayList.add("333");
//        arrayList.add("333");
//        arrayList.add("444");
//

        arrayList.add(new Person("aaa",12));
        arrayList.add(new Person("aaa",12));
        arrayList.add(new Person("bbb",13));
        arrayList.add(new Person("bbb",13));
        arrayList.add(new Person("ccc",14));
        System.out.println(arrayList);

        arrayList = getSingleElement(arrayList);
        System.out.println(arrayList);

        //删除一个不存在的对象
        System.out.println(arrayList.remove(new Person("kkk",20)));
        System.out.println(arrayList.remove(new Person("ccc",14)));

        System.out.println(arrayList);
    }

    private static ArrayList getSingleElement(ArrayList arrayList) {
        //定义一个临时容器
        ArrayList tempArrayList = new ArrayList();
        //迭代arrayList
        Iterator it = arrayList.iterator();
        while (it.hasNext()){
            Object object = it.next();
            //判断被迭代的元素是否在临时容器中存在
            if (!tempArrayList.contains(object)){
                tempArrayList.add(object);
            }
        }
        return tempArrayList;
    }
}

class Person{
    private String name;
    private Integer age;

    public Person(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public boolean equals(Object obj) {
        //if (this == obj){
            //return true;
        //}
        if (!(obj instanceof Person)){
            throw new ClassCastException("类型错误");
        }
        Person p = (Person)obj;
        return this.name.equals(p.name)&&this.age == p.age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}